﻿function scrollToTop()
{
    window.scrollTo(0,0);
}

function loadTabClicked(idContent, url) {
	if ((document.getElementById(idContent).style.display == '') && ($("#" + idContent).children().length > 0)) {
		document.getElementById(idContent).style.display = 'none';
	}
	else {
		document.getElementById(idContent).style.display = '';
		$("#" + idContent).load(url);        
	}    
}

function changeVendorLevel(actionUrl, level) 
{
    if ($("#content").hasClass("use_menu_vendor_details_expanded")) 
    {
        showLoading();
        document.location.href = actionUrl + "?vendorName=" + $("#title" + level).html().trim() + "&vendorLevel=" + level;
    }
}

function toggleVendorDetailsMenu(vendorLevel) {
    if ($("#vendorDetailsMenuContainer").length > 0) {

        //This is because the title of the bottom must be the same that is in the title
        var aux = $("#titleL1").html();
        $("#titleL1").html($("#title" + vendorLevel).html());
        $("#title" + vendorLevel).html(aux);

        $("#content").toggleClass("use_menu_vendor_details_expanded" + "_" + vendorLevel);
        $("#content").toggleClass("use_menu_vendor_details_expanded");


        $("#titleL1").toggleClass("titleVendorDetailsSelected");

        $("#vendorDetailsMenuContainer").toggle();
        $("#vendorDetailsPickerOnMenu").toggle();
        $("#vendorDetailsPicker").toggle();
    }
}

function toggleMenu() {
	$('#profileMenuContainer').toggle();
	$('#menuContainer').toggle();

	if ($("#content").hasClass("use_menu_expanded")) {
		$("#content").removeClass("use_menu_expanded");
		$("#headerContainer").removeClass("use_menu_expanded");
		$("#showMenu").removeClass("use_menu_expanded");
		$("#showMenuClosed").show();
		$("#showMenuOpen").hide();

		if ($("#vendorDetailsMenuContainer").length > 0) 
        {
            $("#vendorDetailsMenuContainer").removeClass("use_menu_expanded");
		}
	}
	else {
		$("#content").addClass("use_menu_expanded");
		$("#headerContainer").addClass("use_menu_expanded");
		$("#showMenu").addClass("use_menu_expanded");
		$("#showMenuClosed").hide();
		$("#showMenuOpen").show();

		if ($("#vendorDetailsMenuContainer").length > 0) 
        {
            $("#vendorDetailsMenuContainer").addClass("use_menu_expanded");
        }
	 }
}

function hideMenu() {
    if ($("#content").hasClass("use_menu_expanded")) {
        toggleMenu();
    }
}

function onFocusContent() {
    hideMenu();
}

function hideMenuOnFocusOut(evt) {
    var target = $(evt.target);
    if (target.parents('header').length == 0 && target.parents('#showMenu').length == 0) {
        hideMenu();
    }

    if (target.parents('#searchContainer').length == 0 && target.parents('#searchButton').length == 0 && target.parents('#searchContent').length == 0) {
        hideSearchContainer();
    }
}

function interactFilters(expandUrl, collapsedUrl) {

	if ($("#content").hasClass("use_sidebar_collapsed")) {
		$("#content").removeClass("use_sidebar_collapsed");
		$("#content").addClass("use_sidebar_expanded");
		$("#filtersContainer").addClass("filtersContainerExpanded");
		//$("#filtersButtonImage").attr("src", expandUrl);
	}
	else {
		$("#content").removeClass("use_sidebar_expanded");
		$("#content").addClass("use_sidebar_collapsed");
		$("#filtersContainer").removeClass("filtersContainerExpanded")
		//$("#filtersButtonImage").attr("src", collapsedUrl);
	}
}



function resizeDataTable(idDataTable, idContainerHeightReplicable) {

    $("#" + idDataTable).height($("#" + idContainerHeightReplicable).height() - 46);

    $(window).resize(function () {
        $("#" + idDataTable).height($("#" + idContainerHeightReplicable).height() - 46);
    });    
}

function resizeChartForVendorDetails(vendorDetailsView, currentViewId, chartObject, idChart, idContainer, resizeHeightValue, hasFilterTray, firstTime) {

    if (firstTime) 
    {
        $(window).resize(function () 
        {
            if ($("#" + currentViewId).val() == vendorDetailsView) 
            {
                chartObject.setSize(
			        $("#" + idChart).width(),
			        $("#" + idContainer).height() * resizeHeightValue,
			        false
		        );
            }
        });

        if (hasFilterTray) {
            $("#filtersButton").click(function () {
                if ($("#" + currentViewId).val() == vendorDetailsView) {
                    chartObject.setSize(
			        $("#" + idChart).width(),
			        $("#" + idContainer).height() * resizeHeightValue,
			        false);
                }
            });
        }
    }

    chartObject.setSize(
		$("#" + idChart).width(),
		$("#" + idContainer).height() * resizeHeightValue,
		false
	);
}



function resizeChart(chartObject, idChart, idContainer, resizeHeightValue, hasFilterTray) {

	$(window).resize(function () {
		chartObject.setSize(
			$("#" + idChart).width(),
			$("#" + idContainer).height() * resizeHeightValue,
			false
		);
	});

	if (hasFilterTray) {
		$("#filtersButton").click(function () {
			chartObject.setSize(
				$("#" + idChart).width(),
				$("#" + idContainer).height() * resizeHeightValue,
				false
			);
		});
    }

    chartObject.setSize(
		    $("#" + idChart).width(),
		    $("#" + idContainer).height() * resizeHeightValue,
		    false
	    );
}

function menuItemSelected(idMenuItem) {
	$(".menuItem").removeClass("menuItemSelected");
	$("#" + idMenuItem).addClass("menuItemSelected");
	$("#" + idMenuItem + "Selected").show();
}

function unselectMenuItem(idMenuItem) {
    $("#" + idMenuItem + "Selected").hide();
}


function redirectToPage(actionPage) {
	window.location.href = actionPage;
}

function hideSeparator(idSeparatorMenu) {
	$("#" + idSeparatorMenu).hide();
}

function showSeparator(idSeparatorMenu) {
	$("#" + idSeparatorMenu).show();
}

function showLoadingBottom() {
    $("#loadingFrameBottom").show();
}

function hideLoadingBottom() {
    $("#loadingFrameBottom").hide();
}

function showLoading() {
    $("#loadingFrame").show();
}

function hideLoading() {
    $("#loadingFrame").hide();
}

function showGlossary(actionUrl) {
    $("#glossaryFrame").load(actionUrl);
    $("#glossaryFrame").show();
}

function hideGlossary() {
    $("#glossaryFrame").hide();
}

function goToBackPage() {
    window.history.go(-1);
}

function getNameShorten(value, maxLength, sentenceEnd) {
    if (value.length > maxLength)
        return value.substring(0, maxLength) + sentenceEnd;

    return value;
}

/****************** DATE EXTENTIONS BEGIN **********************/

Date.prototype.getMonthName = function (lang) {
    lang = lang && (lang in Date.locale) ? lang : 'en';
    return Date.locale[lang].month_names[this.getMonth()];
};

Date.prototype.getMonthNameShort = function (lang) {
    lang = lang && (lang in Date.locale) ? lang : 'en';
    return Date.locale[lang].month_names_short[this.getMonth()];
};

Date.locale = {
    en: {
        month_names: ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'],
        month_names_short: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
    }
};

/****************** DATE EXTENTIONS END **********************/

function onGenericColumnHeaderSort(obj, ascKey, descKey, descImg, ascImg, idHiddenSortOrder, idHiddenSortColumn, idSortedColumnImage) {
    var sortOrder = $('#' + idHiddenSortOrder).val();
    var sortColumn = $('#' + idHiddenSortColumn).val();
    var colName = $(obj).attr('name');
    
    if (colName == sortColumn) {
        if (sortOrder == ascKey) {
            $('#' + idHiddenSortOrder).val(descKey);
            $("#" + idSortedColumnImage).attr("src", descImg);
        } else {
            $('#' + idHiddenSortOrder).val(ascKey);
            $("#" + idSortedColumnImage).attr("src", ascImg);
        }
    }
    else {
        $('#' + idHiddenSortOrder).val(ascKey);
        $('#' + idHiddenSortColumn).val(colName);
    }
}

function onNavItemSelected(navItemId) {
    $(".navitem").removeClass("selectedNavItemVendorDetails");
    $("#" + navItemId).addClass("selectedNavItemVendorDetails");

    $(".navitem div[id=hoverContainer]").hide();
    $(".navitem div[id=normalContainer]").show();

    $("#" + navItemId + " div[id=normalContainer]").hide();
    $("#" + navItemId + " div[id=hoverContainer]").show();
}


/****************************************************************************/

function toggleTooltips() {
    $('.isNotPercentage').toggle();
    $('.isPercentage').toggle();
}

/******************************* JSON ****************************************/

function hideSearchContainer() 
{
    $("#searchContainer").hide();
}

function showSearchContainer() 
{
    $("#searchContainer").show();    
}

function performSearch(searchText, actionSearch) 
{
	var data = { 'searchText': searchText };
	actionAsyncCall(data, actionSearch, 'html', 'searchList',
        function () {
            OnFilterCompleted();
            if (!manageFilterLoadingExternal) {
                hideLoading();
            }
        },
        function () { hideLoading(); });
}